﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using MySql.Data.MySqlClient;
using MySql.Data.Entity;

namespace MealCardManagement.Data
{
    [DbConfigurationType(typeof(MySqlEFConfiguration))]
    public class MCMContext : DbContext
    {
        public MCMContext()
            :this("MealCardManagement")
        { }

        public MCMContext(string dataBaseName)
            : base(BuildConnetcString(dataBaseName))
        { }

        public DbSet<User> Users { get; set; }
        public DbSet<MealCard> MealCards { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Configurations.AddFromAssembly(typeof(MCMContext).Assembly);
            base.OnModelCreating(modelBuilder);
        }

        static private string BuildConnetcString(string dataBase)
        {
            var build = new MySqlConnectionStringBuilder();

            build.Server = "localhost";
            build.Database = dataBase;
            build.UserID = "root";
            build.Password = null;
            build.CharacterSet = "utf8";

            return build.ToString();
        }
    }
}
